package com.century.sjt.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.century.sjt.R;
import com.century.sjt.entity.Commodit;
import com.century.sjt.util.Constant;
import com.century.sjt.waterfall.dodowaterfall.widget.ScaleImageView;
import com.century.sjt.waterfall.imageloader.ImageLoader;
import com.century.sjt.waterfall.util.ImageFetcher;
import com.century.sjt.waterfall.view.XListView;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by 炳基 on 2016/8/26.
 */
public class CommodityAdapter extends BaseAdapter {
    private Activity mContext;
    private List<Commodit> mInfos;
    private XListView mListView;
    private Handler mHandler;
    private ImageFetcher imageFetcher;
    private ImageLoader mLoader;

    public CommodityAdapter(Activity context, XListView xListView, Handler mHandler, ImageLoader mLoader,List<Commodit> mInfos) {
        this.mContext = context;
        this.mInfos = mInfos;
        this.mListView = xListView;
        this.mHandler = mHandler;
        this.mLoader = mLoader;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        final Commodit duitangInfo = mInfos.get(position);

        if (convertView == null) {
            LayoutInflater layoutInflator = LayoutInflater.from(parent.getContext());
            convertView = layoutInflator.inflate(R.layout.infos_list, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.commodity_pic);
            holder.contentView = (TextView) convertView.findViewById(R.id.commodity_title);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.commodity_price);
            holder.icon_select2 = (TextView) convertView.findViewById(R.id.icon_select2);
            holder.ll_selected = (LinearLayout) convertView.findViewById(R.id.ll_selected);
            Typeface iconfont = Typeface.createFromAsset(mContext.getAssets(), "iconfont.ttf");
            holder.icon_select2.setTypeface(iconfont);

//            DisplayMetrics dm = new DisplayMetrics();
//            mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
//            int screenWidth = dm.widthPixels;//屏幕宽
//            LinearLayout.LayoutParams imgvwMargin = new LinearLayout.LayoutParams(screenWidth / 2, ViewGroup.LayoutParams.WRAP_CONTENT);
//            holder.imageView.setLayoutParams(imgvwMargin);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (mInfos.get(position).isSelect){
            holder.icon_select2.setText(mContext.getResources().getString(R.string.icon_select2));
        }else {
            holder.icon_select2.setText(mContext.getResources().getString(R.string.icon_unselected));
        }
        holder.ll_selected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mInfos.get(position).isSelect){
                    mInfos.get(position).setSelect(false);
                    holder.icon_select2.setText(mContext.getResources().getString(R.string.icon_unselected));
                }else {
                    mInfos.get(position).setSelect(true);
                    holder.icon_select2.setText(mContext.getResources().getString(R.string.icon_select2));
                }

                Message msg = new Message();
                msg.what = 1;
                mHandler.sendMessage(msg);

            }
        });
//        Bitmap bitmap = imageFetcher.getBitmap(duitangInfo.getIsrc(),holder.imageView);
//        holder.imageView.setImageWidth(bitmap.getWidth());
//        holder.imageView.setImageHeight(bitmap.getHeight());
        holder.tvPrice.setText("￥"+ duitangInfo.getPrice());
        holder.contentView.setText(duitangInfo.getGoodName());
//        mLoader.DisplayImage(duitangInfo.getIsrc(),holder.imageView);
//        String url = Constant.BaseWebHost + duitangInfo.getMainImageUrl();
//        Log.e("img",url);
        mLoader.DisplayImage(duitangInfo.getMainImageUrl(),holder.imageView);
//        holder.imageView.setImageBitmap(bitmap);
//        imageFetcher.loadImage(duitangInfo.getIsrc(),holder.imageView);

//        Glide.with(mContext).load(Constant.BaseHost + duitangInfo.getMainImageUrl()).into(holder.imageView);
//        Glide.with(mContext).load(Constant.BaseHost + duitangInfo.getMainImageUrl())
//                .asBitmap()
//                .fitCenter()
//                .into(new SimpleTarget<Bitmap>(250,250) {
//
//                    @Override
//                    public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
////                        bitmap.getHeight(); //获取bitmap信息，可赋值给外部变量操作，也可在此时行操作。
////                        bitmap.getWidth();
//                        holder.imageView.setImageWidth(bitmap.getWidth());
//                        holder.imageView.setImageHeight(bitmap.getHeight());
//                        holder.imageView.setImageBitmap(bitmap);
//                        Log.e("height",bitmap.getHeight()+"");
//                        Message msg = new Message();
//                        msg.what = 1;
//                        mHandler.sendMessage(msg);
//                    }
//
//
//                });
        return convertView;
    }

    private static class ViewHolder {
        ImageView imageView;
        TextView contentView;
        TextView tvPrice;
        TextView icon_select2;
        LinearLayout ll_selected;
    }

    @Override
    public int getCount() {
        return mInfos.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mInfos.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

//    public void addItemLast(List<Commodit> datas) {
//        mInfos.addAll(datas);
//
//    }
//    public void addItemTop(List<Commodit> datas) {
//        mInfos.clear();
//        mInfos.addAll(datas);
////        for (Commodit info : datas) {
////            mInfos.addFirst(info);
////        }
//    }
}
